package com.hdy.common.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

public class NetworkConnectionReceiver extends BroadcastReceiver {

    private static final int ONE_SECOND = 1000;

    private Handler mHandler = new Handler();

    private int mLastNetworkType = -1;
    private boolean mLastNetworkAvailable;
    private boolean mCurrentNetworkAvailable;

    @Override
    public void onReceive(Context context, Intent intent) {

        if (isBroadcastDuplicate(context)) {
            return;
        }

        if (isNetworkDisconnect(context)) {
            NetworkConnectionManager.getInstance().notifyNetworkDisconnected();
            return;
        }

        if (canNotify(context)) {
            NetworkConnectionManager.getInstance().notifyObserver();
        }

        if (isWLanConnect(context)) {
            NetworkConnectionManager.getInstance().notifyNetworkConnectChange(NetworkConnectState.CONNECT_WLAN);
            return;
        }

        if (isMobileConnect(context)) {
            NetworkConnectionManager.getInstance().notifyNetworkConnectChange(NetworkConnectState.CONNECT_MOBILE);
            return;
        }

        if (isBlueToothConnect(context)) {
            NetworkConnectionManager.getInstance().notifyNetworkConnectChange(NetworkConnectState.CONNECT_BLUETOOTH);
            return;
        }

    }

    private boolean canNotify(Context context) {
        boolean isAviable = NetworkUtils.isAvailable(context);
        if (mLastNetworkAvailable == isAviable) {
            return false;
        }

        mLastNetworkAvailable = isAviable;
        return isAviable;
    }

    private void delayNotifyDisconnect() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mCurrentNetworkAvailable) {
                    mHandler.removeCallbacks(this);
                    return;
                }
                NetworkConnectionManager.getInstance()
                        .notifyNetworkConnectChange(NetworkConnectState.DISCONNECT);
            }
        }, ONE_SECOND);
    }

    private boolean isBroadcastDuplicate(Context context) {
        int netType = NetworkUtils.getNetworkState(context);
        if (mLastNetworkType == netType) {
            return true;
        }
        mLastNetworkType = netType;
        return false;
    }

    private boolean isNetworkDisconnect(Context context) {
        int netType = NetworkUtils.getNetworkState(context);
        if (netType == NetworkUtils.TYPE_NO_NETWORK) {
            mCurrentNetworkAvailable = NetworkUtils.isAvailable(context);
            if (mCurrentNetworkAvailable) {
                return false;
            }
            return true;
        }
        return false;
    }

    private boolean isWLanConnect(Context context) {
        return isConnetByTargetType(context, NetworkUtils.TYPE_WIFI);
    }

    private boolean isMobileConnect(Context context) {
        return isConnetByTargetType(context, NetworkUtils.TYPE_MOBILE);
    }

    private boolean isBlueToothConnect(Context context) {
        return isConnetByTargetType(context, NetworkUtils.TYPE_BLUETOOTH);
    }

    private boolean isConnetByTargetType(Context context, int targetType) {
        int netType = NetworkUtils.getNetworkState(context);
        if (netType == targetType) {
            mCurrentNetworkAvailable = NetworkUtils.isAvailable(context);
            if (mCurrentNetworkAvailable) {
                return true;
            }
            return false;
        }
        return false;
    }

}